package com.gong.chooselesson.controller;

import com.gong.chooselesson.pojo.Lesson;
import com.gong.chooselesson.pojo.Teacher;
import com.gong.chooselesson.service.LessonService;
import com.gong.chooselesson.service.SlService;
import com.gong.chooselesson.service.StudentService;
import com.gong.chooselesson.service.TeacherService;
import com.gong.chooselesson.utils.result.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author gongchang
 * @Date 2021-07-27 16:39
 */
@RestController
@Api(tags = "选课服务")
@CrossOrigin
public class ChooseController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    StudentService studentService;
    @Autowired
    SlService slService;
    @Autowired
    LessonService lessonService;
    @Autowired
    Jedis writeJedis;
    @Autowired
    Jedis readJedis;

    //展示课程列表
    @GetMapping("/list")
    public Result getList() {
        Map res = new HashMap();
        List<Teacher> list = teacherService.list();
        List<Lesson> list1 = lessonService.list();
        res.put("teacher", list);
        res.put("lesson", list1);

        return Result.ok(res);
    }

    //选课
    @GetMapping("/choose/{lno}/{sno}")
    public Result chooseLesson(@PathVariable("lno") int lno, @PathVariable("sno") int sno) {
        //1.判断课程余量
        int lessonCount = lessonService.getLessonCount(lno);
        if (lessonCount <= 0) {
            return Result.fail().message("课余量为0");
        }
        //2.判断课程是否冲突
        boolean is_crotch = slService.iscroth(lno, sno);
        if (!is_crotch) {
            return Result.fail().message("选课冲突");
        }
        //3.写入数据
        //3.1获取锁,再一次判断余量是否充足
        Long setnx = 0L;
        for (int i = 5; i > 0; i--) {
            setnx = writeJedis.setnx("lock", "lock");
            if (setnx == 1) {
                break;
            }
        }
        if (setnx == 0) {
            return Result.fail().message("选课失败请重试");
        }
        int count = lessonService.getLessonCount(lno);
        if (lessonCount <= 0) {
            writeJedis.del("lock");
            return Result.fail().message("课余量为0");
        }

        //3.2开启事务
        Transaction multi = writeJedis.multi();
        //3.3写数据
        Lesson lesson = lessonService.getById(lno);
        try {
            multi.lpush("sno:lessons" , String.valueOf(lno));
            multi.decr("lesson:" + lno + ":l_left");
            multi.setbit("sno:" + sno, lesson.getLNo(), true);
            //3.4提交事务
            multi.exec();
        } catch (Exception e) {
            writeJedis.del("lock");//3.5释放锁
            e.printStackTrace();
            multi.discard();
        }

        writeJedis.del("lock");
        return Result.ok();
    }
}

























